<?php
class Account extends Controller
{
    protected function _preDispatch($action)
    {
        if ($action != 'register')
        {
            if (!$this->_session->get('customerId'))
            {
                $this->responseRedirect('login/login');
            }
        }

        $this->loadChildControllers(array(
            'header' => 'module/Header',
            'footer' => 'module/Footer',

            // right sidebar
            'accountMenu' => 'module/AccountMenu'
        ));
    }

    public function actionIndex()
    {
        $this->setTitle('Thông tin khách hàng');

        $this->setBreadcrumb(array(
            array(
                'title' => 'Trang chủ',
                'link' => Link::build(''),
                'index' => true,
            ),
            array(
                'title' => 'Tài khoản',
                'link' => Link::build('account'),
            ),
            array(
                'title' => 'Thông tin khách hàng',
                'link' => Link::build('account/index'),
            )
        ));

        $customerModel = $this->_loader->model('CustomerModel');

        $customer = $customerModel->getCustomerById($this->_session->get('customerId'));

        $viewParams = array(
            'customer' => $customer
        );

        $this->_loader->helper('Html');

        return $this->responseView('account/information', $viewParams, 'layout/right_sidebar');
    }

    public function actionUpdateInformation()
    {
        $customerModel = $this->_loader->model('CustomerModel');

        $customer = $customerModel->getCustomerById($this->_session->get('customerId'));

        $password = $this->_input->filterSingle('password', Input::STRING);

        $data = $this->_input->filter(array(
            'name' => Input::STRING,
            'email' => Input::STRING,
            'phone' => Input::STRING,
            'birthday' => Input::STRING,
        ));

        $data['birthday'] = strtotime($data['birthday']);

        $data['customer_id'] = $this->_session->get('customerId');

        if (md5($password) != $customer['password'])
        {
            $this->setBreadcrumb(array(
                array(
                    'title' => 'Trang chủ',
                    'link' => Link::build(''),
                    'index' => true,
                ),
                array(
                    'title' => 'Tài khoản',
                    'link' => Link::build('account'),
                ),
                array(
                    'title' => 'Thông tin khách hàng',
                    'link' => Link::build('account/index'),
                )
            ));

            $this->loadChildControllers(array(
                'header' => 'module/Header',
                'footer' => 'module/Footer',

                // right sidebar
                'accountMenu' => 'module/AccountMenu'
            ));

            $this->_loader->helper('Html');

            $viewParams = array(
                'customer' => $data,
                'error' => 'Mật khẩu không chính xác.',
            );

            return $this->responseView('account/information', $viewParams, 'layout/right_sidebar');
        }

        $customerModel = $this->_loader->model('CustomerModel');

        $customerModel->update($data);

        return $this->responseRedirect('account/index');
    }

    public function actionChangePassword()
    {
        $this->setBreadcrumb(array(
            array(
                'title' => 'Trang chủ',
                'link' => Link::build(''),
                'index' => true,
            ),
            array(
                'title' => 'Tài khoản',
                'link' => Link::build('account'),
            ),
            array(
                'title' => 'Thay đổi mật khẩu',
                'link' => Link::build('account/index'),
            )
        ));

        $do = $this->_input->filterSingle('do', Input::STRING);

        if ($do != 'change_password')
        {
            return $this->responseView('account/change_password', array(), 'layout/right_sidebar');
        }

        $input = $this->_input->filter(array(
            'old_password' => Input::STRING,
            'new_password' => Input::STRING,
            'confirm_password' => Input::STRING
        ));

        if ($input['new_password'] != $input['confirm_password'])
        {
            $error = 'Mật khẩu xác nhận không khớp.';
        }
        else
        {
            // Kiểm tra mật khẩu cũ có đúng không
            $customerModel = $this->_loader->model('CustomerModel');
            $customerId = $this->_session->get('customerId');
            $customer = $customerModel->getCustomerById($customerId);

            if ($customer['password'] != md5($input['old_password']))
            {
                $error = 'Mật khẩu cũ không chính xác.';
            }
            else
            {
                $data = array(
                    'customer_id' => $customerId,
                    'password' => md5($input['new_password'])
                );

                $customerModel->update($data);

                return $this->responseRedirect('account/index');
            }
        }

        $viewParams = array(
            'error' => $error
        );

        return $this->responseView('account/change_password', $viewParams, 'layout/right_sidebar');
    }
}
